from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from ui_Cal import Ui_Dialog
class Cal(Ui_Dialog):
    def __init__(self):
        app = QtWidgets.QApplication(sys.argv)
        Dialog = QtWidgets.QDialog()
        self.setupUi(Dialog)
        self.bitbtn = []
        self.bitbtn.append(self.pushButton_0)
        self.bitbtn.append(self.pushButton_1)
        self.bitbtn.append(self.pushButton_2)
        self.bitbtn.append(self.pushButton_3)
        self.bitbtn.append(self.pushButton_4)
        self.bitbtn.append(self.pushButton_5)
        self.bitbtn.append(self.pushButton_6)
        self.bitbtn.append(self.pushButton_7)
        self.bitbtn.append(self.pushButton_8)
        self.bitbtn.append(self.pushButton_9)
        self.bitbtn.append(self.pushButton_10)
        self.bitbtn.append(self.pushButton_11)
        self.bitbtn.append(self.pushButton_12)
        self.bitbtn.append(self.pushButton_13)
        self.bitbtn.append(self.pushButton_14)
        self.bitbtn.append(self.pushButton_15)
        self.bitbtn.append(self.pushButton_16)
        self.bitbtn.append(self.pushButton_17)
        self.bitbtn.append(self.pushButton_18)
        self.bitbtn.append(self.pushButton_19)
        self.bitbtn.append(self.pushButton_20)
        self.bitbtn.append(self.pushButton_21)
        self.bitbtn.append(self.pushButton_22)
        self.bitbtn.append(self.pushButton_23)
        self.bitbtn.append(self.pushButton_24)
        self.bitbtn.append(self.pushButton_25)
        self.bitbtn.append(self.pushButton_26)
        self.bitbtn.append(self.pushButton_27)
        self.bitbtn.append(self.pushButton_28)
        self.bitbtn.append(self.pushButton_29)
        self.bitbtn.append(self.pushButton_30)
        self.bitbtn.append(self.pushButton_31)
        
        self.pushButton_0.clicked.connect(self.button0_clicked)
        self.pushButton_1.clicked.connect(self.button1_clicked)
        self.pushButton_2.clicked.connect(self.button2_clicked)
        self.pushButton_3.clicked.connect(self.button3_clicked)
        self.pushButton_4.clicked.connect(self.button4_clicked)
        self.pushButton_5.clicked.connect(self.button5_clicked)
        self.pushButton_6.clicked.connect(self.button6_clicked)
        self.pushButton_7.clicked.connect(self.button7_clicked)
        self.pushButton_8.clicked.connect(self.button8_clicked)
        self.pushButton_9.clicked.connect(self.button9_clicked)
        self.pushButton_10.clicked.connect(self.button10_clicked)
        self.pushButton_11.clicked.connect(self.button11_clicked)
        self.pushButton_12.clicked.connect(self.button12_clicked)
        self.pushButton_13.clicked.connect(self.button13_clicked)
        self.pushButton_14.clicked.connect(self.button14_clicked)
        self.pushButton_15.clicked.connect(self.button15_clicked)
        self.pushButton_16.clicked.connect(self.button16_clicked)
        self.pushButton_17.clicked.connect(self.button17_clicked)
        self.pushButton_18.clicked.connect(self.button18_clicked)
        self.pushButton_19.clicked.connect(self.button19_clicked)
        self.pushButton_20.clicked.connect(self.button20_clicked)
        self.pushButton_21.clicked.connect(self.button21_clicked)
        self.pushButton_22.clicked.connect(self.button22_clicked)
        self.pushButton_23.clicked.connect(self.button23_clicked)
        self.pushButton_24.clicked.connect(self.button24_clicked)
        self.pushButton_25.clicked.connect(self.button25_clicked)
        self.pushButton_26.clicked.connect(self.button26_clicked)
        self.pushButton_27.clicked.connect(self.button27_clicked)
        self.pushButton_28.clicked.connect(self.button28_clicked)
        self.pushButton_29.clicked.connect(self.button29_clicked)
        self.pushButton_30.clicked.connect(self.button30_clicked)
        self.pushButton_31.clicked.connect(self.button31_clicked)
        self.hexEdit.editingFinished.connect(self.hex_edit_finished)
        self.decEdit.editingFinished.connect(self.dec_edit_finished)
        self.pushButton_clear.clicked.connect(self.pushButton_clear_clicked)
        self.pushButton_fan.clicked.connect(self.pushButton_fan_clicked)
        self.pushButton_left.clicked.connect(self.pushButton_left_clicked)
        self.pushButton_right.clicked.connect(self.pushButton_right_clicked)
        self.pushButton.setVisible(0)
        Dialog.show()
        sys.exit(app.exec_())
    

    def pushButton_left_clicked(self):
        hexstr = self.hexEdit.text()
        hexdat = int(hexstr,16)
        hexdat = hexdat << 1
        hexdat = 0x100000000 + hexdat
        binstr = bin(hexdat)[3:]
        for i in range(32):
            bitsel = binstr[i:(i+1)]
            if bitsel == "1":
                self.bitbtn[31-i].setText("1")
                self.bitbtn[31-i].setStyleSheet("background-color: rgb(255, 255, 0);")
            else :
                self.bitbtn[31-i].setText("0")
                self.bitbtn[31-i].setStyleSheet("background-color: rgb(225, 225, 225);")
        decstr = str(hexdat-0x100000000).upper()
        hexstr = hex(hexdat)[3:].upper()
        self.decEdit.setText(decstr)   
        self.hexEdit.setText(hexstr)         
        pass

    def pushButton_right_clicked(self):
        hexstr = self.hexEdit.text()
        hexdat = int(hexstr,16)
        hexdat = hexdat >> 1
        hexdat = 0x100000000 + hexdat
        binstr = bin(hexdat)[3:]
        for i in range(32):
            bitsel = binstr[i:(i+1)]
            if bitsel == "1":
                self.bitbtn[31-i].setText("1")
                self.bitbtn[31-i].setStyleSheet("background-color: rgb(255, 255, 0);")
            else :
                self.bitbtn[31-i].setText("0")
                self.bitbtn[31-i].setStyleSheet("background-color: rgb(225, 225, 225);")
        decstr = str(hexdat-0x100000000).upper()
        hexstr = hex(hexdat)[3:].upper()
        self.decEdit.setText(decstr)   
        self.hexEdit.setText(hexstr) 
        pass

    def pushButton_clear_clicked(self):
        self.hexEdit.setText("00000000")
        self.decEdit.setText("0000000000")
        for i in range(32):
            self.bitbtn[i].setText("0")
            self.bitbtn[i].setStyleSheet("background-color: rgb(225, 225, 225);")            

    def pushButton_fan_clicked(self):
        hexstr = self.hexEdit.text()
        hexdat = int(hexstr,16)
        hexdat = 0x100000000 - hexdat - 1
        hexdat = 0x100000000 + hexdat
        binstr = bin(hexdat)[3:]
        for i in range(32):
            bitsel = binstr[i:(i+1)]
            if bitsel == "1":
                self.bitbtn[31-i].setText("1")
                self.bitbtn[31-i].setStyleSheet("background-color: rgb(255, 255, 0);")
            else :
                self.bitbtn[31-i].setText("0")
                self.bitbtn[31-i].setStyleSheet("background-color: rgb(225, 225, 225);")
        decstr = str(hexdat-0x100000000).upper()
        hexstr = hex(hexdat)[3:].upper()
        self.decEdit.setText(decstr)   
        self.hexEdit.setText(hexstr)     
        pass


        


    def hex_edit_finished(self):
        hexstr = self.hexEdit.text()
        hexdat =  0x100000000+int(hexstr,16)
        binstr = bin(hexdat)[3:]
        for i in range(32):
            bitsel = binstr[i:(i+1)]
            if bitsel == "1":
                self.bitbtn[31-i].setText("1")
                self.bitbtn[31-i].setStyleSheet("background-color: rgb(255, 255, 0);")
            else :
                self.bitbtn[31-i].setText("0")
                self.bitbtn[31-i].setStyleSheet("background-color: rgb(225, 225, 225);")
        decstr = str(hexdat-0x100000000).upper()
        self.decEdit.setText(decstr)
        

    def dec_edit_finished(self):
        decstr = self.decEdit.text()
        hexdat =  0x100000000+int(decstr)
        binstr = bin(hexdat)[3:]
        for i in range(32):
            bitsel = binstr[i:(i+1)]
            if bitsel == "1":
                self.bitbtn[31-i].setText("1")
                self.bitbtn[31-i].setStyleSheet("background-color: rgb(255, 255, 0);")
            else :
                self.bitbtn[31-i].setText("0")
                self.bitbtn[31-i].setStyleSheet("background-color: rgb(225, 225, 225);")
        hexstr = hex(hexdat)[3:].upper()
        self.hexEdit.setText(hexstr)

    def bit_button_clicked(self,btn,numb):
        hexstr = self.hexEdit.text()
        hexdat =  int(hexstr,16)
        if btn.text() == "0":
            btn.setText("1")
            btn.setStyleSheet("background-color: rgb(255, 255, 0);")
            hexdat = hexdat + (1<<numb)
        else :
            btn.setText("0")
            btn.setStyleSheet("background-color: rgb(225, 225, 225);")
            hexdat = hexdat - (1<<numb)
        hexstr = hex(0x100000000+hexdat)[3:11].upper()
        decstr = str(hexdat).upper()
        self.hexEdit.setText(hexstr)
        self.decEdit.setText(decstr)
            

    def button0_clicked(self):
        self.bit_button_clicked(self.pushButton_0,0)

    def button1_clicked(self):
        self.bit_button_clicked(self.pushButton_1,1)
    
    def button2_clicked(self):
        self.bit_button_clicked(self.pushButton_2,2)

    def button3_clicked(self):
        self.bit_button_clicked(self.pushButton_3,3)

    def button4_clicked(self):
        self.bit_button_clicked(self.pushButton_4,4)

    def button5_clicked(self):
        self.bit_button_clicked(self.pushButton_5,5)

    def button6_clicked(self):
        self.bit_button_clicked(self.pushButton_6,6)

    def button7_clicked(self):
        self.bit_button_clicked(self.pushButton_7,7)

    def button8_clicked(self):
        self.bit_button_clicked(self.pushButton_8,8)

    def button9_clicked(self):
        self.bit_button_clicked(self.pushButton_9,9)

    def button10_clicked(self):
        self.bit_button_clicked(self.pushButton_10,10)

    def button11_clicked(self):
        self.bit_button_clicked(self.pushButton_11,11)

    def button12_clicked(self):
        self.bit_button_clicked(self.pushButton_12,12)

    def button13_clicked(self):
        self.bit_button_clicked(self.pushButton_13,13)

    def button14_clicked(self):
        self.bit_button_clicked(self.pushButton_14,14)

    def button15_clicked(self):
        self.bit_button_clicked(self.pushButton_15,15)

    def button16_clicked(self):
        self.bit_button_clicked(self.pushButton_16,16)

    def button17_clicked(self):
        self.bit_button_clicked(self.pushButton_17,17)

    def button18_clicked(self):
        self.bit_button_clicked(self.pushButton_18,18)

    def button19_clicked(self):
        self.bit_button_clicked(self.pushButton_19,19)

    def button20_clicked(self):
        self.bit_button_clicked(self.pushButton_20,20)

    def button21_clicked(self):
        self.bit_button_clicked(self.pushButton_21,21)

    def button22_clicked(self):
        self.bit_button_clicked(self.pushButton_22,22)

    def button23_clicked(self):
        self.bit_button_clicked(self.pushButton_23,23)

    def button24_clicked(self):
        self.bit_button_clicked(self.pushButton_24,24)

    def button25_clicked(self):
        self.bit_button_clicked(self.pushButton_25,25)

    def button26_clicked(self):
        self.bit_button_clicked(self.pushButton_26,26)

    def button27_clicked(self):
        self.bit_button_clicked(self.pushButton_27,27)

    def button28_clicked(self):
        self.bit_button_clicked(self.pushButton_28,28)

    def button29_clicked(self):
        self.bit_button_clicked(self.pushButton_29,29)

    def button30_clicked(self):
        self.bit_button_clicked(self.pushButton_30,30)

    def button31_clicked(self):
        self.bit_button_clicked(self.pushButton_31,31)


if __name__ == "__main__":
    a=Cal()
    
